package com.cdivtc.management.service;

import com.cdivtc.management.controller.StudentSelectionController;
import com.cdivtc.management.dao.*;
import com.cdivtc.management.pojo.*;
import com.cdivtc.management.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class StudentSelectionService {

    @Autowired
    private StudentSelectionMapper studentSelectionMapper;
    @Autowired
    private TopicTitleMapper topicTitleMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherReleaseTaskMapper teacherReleaseTaskMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private StuTaskStatusMapper stuTaskStatusMapper;

    //学生选择毕设方向 确定题目
    public synchronized StudentSelection add(StudentSelection sss) {
        Example e = new Example(TopicTitle.class);
        Example.Criteria c = e.createCriteria();
        c.andEqualTo("id", sss.getTopicId());
        TopicTitle topicTitle1 = topicTitleMapper.selectOneByExample(e);
        String count = count(topicTitle1.getTeaId(), sss.getGrade());
        Integer integer = new Integer(count);
        System.out.println("StudentSelectionService line 34= " + integer);
        if (integer < 15) {
            sss.setId(idWorker.nextId() + "");
            sss.setState("0");
            Example example1 = new Example(TopicTitle.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("id", sss.getTopicId());
            TopicTitle topicTitle = topicTitleMapper.selectOneByExample(example1);
            Integer i = new Integer(topicTitle.getCount());
            topicTitle.setCount((i + 1) + "");
            topicTitleMapper.updateByExample(topicTitle, example1);
            int insert = studentSelectionMapper.insert(sss);
            if (insert > 0) {
                return sss;
            } else
                return null;
        } else return null;
    }

    public List<Map<String, Object>> select(String ss) {
        Example example1 = new Example(StudentSelection.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("stuId", ss);
        List<StudentSelection> studentSelections = studentSelectionMapper.selectByExample(example1);
        if (studentSelections != null) {
            List<Map<String, Object>> list = new ArrayList<>();
            for (StudentSelection studentSelection : studentSelections) {
                Map<String, Object> map = new HashMap<>();
                Example example = new Example(TopicTitle.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("id", studentSelection.getTopicId());
                TopicTitle topicTitle = topicTitleMapper.selectOneByExample(example);
                System.out.println("StudentSelectionService line 62= " + topicTitle);

                if (topicTitle == null) continue;

                map.put("studentSelection", studentSelection);
                map.put("topicTitle", topicTitle);
                list.add(map);
            }
            return list;
        } else
            return null;
    }

    public String count(String teaId, String grade) {
        Example example = new Example(TopicTitle.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("teaId", teaId);
        criteria.andEqualTo("grade", grade);
        List<TopicTitle> topicTitles = topicTitleMapper.selectByExample(example);
        int a = 0;
        for (TopicTitle topicTitle : topicTitles) {
            a += new Integer(topicTitle.getCount());
        }
        return a + "";
    }

    public StudentSelection delete(StudentSelection ss) {

        Example example1 = new Example(StudentSelection.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("id", ss.getId());
        int delete = studentSelectionMapper.deleteByExample(example1);


        Example example = new Example(TopicTitle.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", ss.getTopicId());
        TopicTitle topicTitle = topicTitleMapper.selectOneByExample(example);
        topicTitle.setCount((new Integer(topicTitle.getCount()) - 1) + "");//人数减一
        topicTitleMapper.updateByExample(topicTitle, example);
        if (delete > 0) return ss;
        else return null;
    }

    public StudentSelection selStuGradeById(String stuId) {
        Example example1 = new Example(StudentSelection.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("stuId", stuId);
        StudentSelection studentSelection = studentSelectionMapper.selectOneByExample(example1);
        if (studentSelection != null) return studentSelection;
        else return null;
    }

    public StudentSelection updStuGradeById(String stuId,String score) {
        Example example1 = new Example(StudentSelection.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("stuId", stuId);
        StudentSelection studentSelection = studentSelectionMapper.selectOneByExample(example1);
        studentSelection.setScore(score);
        int i = studentSelectionMapper.updateByPrimaryKey(studentSelection);
        if (i >0) return studentSelection;
        else return null;
    }

    public List<Map<String, Object>> selectByTeaId(String teaId) {
        Example example = new Example(StudentSelection.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("teaId", teaId);
        List<StudentSelection> studentSelections = studentSelectionMapper.selectByExample(example);
        System.out.println("StudentSelectionService line 142= " + studentSelections);
        List<Map<String, Object>> list = new ArrayList<>();

        for (StudentSelection studentSelection : studentSelections) {
            Map<String, Object> map = new HashMap<>();

            Student student = studentMapper.selectByPrimaryKey(studentSelection.getStuId());
            System.out.println("StudentSelectionService line 149= " + student);
            Map<String, Object> map1 = studentService.stuInfoById(student.getId());
            map.put("studentSelection", studentSelection);
            map.put("student", map1);
            System.out.println("StudentSelectionService line 153= " + map1);
            list.add(map);
        }
        if (list != null) return list;
        else return null;
    }

    public List<Map<String, Object>> selectByTeaId2(String teaId) {
        Example example = new Example(StudentSelection.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("teaId", teaId);
        criteria.andEqualTo("state", "0");
        List<StudentSelection> studentSelections = studentSelectionMapper.selectByExample(example);

        List<Map<String, Object>> list = new ArrayList<>();

        for (StudentSelection studentSelection : studentSelections) {
            Map<String, Object> map = new HashMap<>();

            Student student = studentMapper.selectByPrimaryKey(studentSelection.getStuId());
            Map<String, Object> map1 = studentService.stuInfoById(student.getId());
            map.put("studentSelection", studentSelection);
            map.put("student", map1);
            list.add(map);
        }
        if (list != null) return list;
        else return null;
    }

    public HashSet<String> selGrade() {//查询年级
        List<StudentSelection> studentSelections = studentSelectionMapper.selectAll();
        HashSet<String> strings = new HashSet<>();
        for (StudentSelection studentSelection : studentSelections) {
            strings.add(studentSelection.getGrade());
        }
        return strings;
    }

    public Boolean update(StudentSelectionController.UpdateStatus updateStatus) {
        System.out.println("StudentSelectionService line 145= " + updateStatus);
        if (updateStatus.getStatus().equals("1")) {
            for (String s : updateStatus.getSsId()) {
                StudentSelection studentSelection = studentSelectionMapper.selectByPrimaryKey(s);
                studentSelection.setState(updateStatus.getStatus());
                int i = studentSelectionMapper.updateByPrimaryKey(studentSelection);
                if (i <= 0) return false;
            }
        } else {
            for (String s : updateStatus.getSsId()) {
                StudentSelection ss1 = studentSelectionMapper.selectByPrimaryKey(s);
                String topicId = ss1.getTopicId();
                Example example = new Example(TopicTitle.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("id", topicId);
                TopicTitle topicTitle = topicTitleMapper.selectOneByExample(example);
                topicTitle.setCount((new Integer(topicTitle.getCount()) - 1) + "");//人数减一
                topicTitleMapper.updateByExample(topicTitle, example);
                studentSelectionMapper.deleteByPrimaryKey(s);
            }
        }

        return true;
    }

    public List<Map<String, Object>> stuSelTopic(String stuId) {
        Example example = new Example(StudentSelection.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stuId", stuId);
        criteria.andEqualTo("state", "1");
        List<StudentSelection> studentSelections = studentSelectionMapper.selectByExample(example);
        List<String> list = new ArrayList<>();
        for (StudentSelection studentSelection : studentSelections) {
            list.add(studentSelection.getTopicId());
        }
        if (list.size() > 0) {
            Example example1 = new Example(TeacherReleaseTask.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andIn("topicId", list);
            criteria1.andEqualTo("state", "1");
            List<TeacherReleaseTask> teacherReleaseTasks = teacherReleaseTaskMapper.selectByExample(example1);
            List<Map<String, Object>> list1 = new ArrayList<>();
            for (TeacherReleaseTask teacherReleaseTask : teacherReleaseTasks) {
                Map<String, Object> map = new HashMap<>();
                TopicTitle topicTitle = topicTitleMapper.selectByPrimaryKey(teacherReleaseTask.getTopicId());
                Teacher teacher = teacherMapper.selectByPrimaryKey(teacherReleaseTask.getTeaId());
                Example e = new Example(StuTaskStatus.class);
                Example.Criteria c = e.createCriteria();
                c.andEqualTo("taskId", teacherReleaseTask.getId());
                c.andEqualTo("stuId", stuId);
                StuTaskStatus stuTaskStatus = stuTaskStatusMapper.selectOneByExample(e);

                map.put("teacherReleaseTask", teacherReleaseTask);
                map.put("topicTitle", topicTitle);
                map.put("teacher", teacher);
                map.put("stuTaskStatus", stuTaskStatus);
                list1.add(map);
            }
            return list1;
        }
        return null;
    }

    public List<Map<String, Object>> stuSelTopicByTopicId(String topicId, String stuId) {
        Example example1 = new Example(TeacherReleaseTask.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("topicId", topicId);
        criteria1.andEqualTo("state", "1");
        List<TeacherReleaseTask> teacherReleaseTasks = teacherReleaseTaskMapper.selectByExample(example1);
        List<Map<String, Object>> list1 = new ArrayList<>();
        for (TeacherReleaseTask teacherReleaseTask : teacherReleaseTasks) {
            Map<String, Object> map = new HashMap<>();
            TopicTitle topicTitle = topicTitleMapper.selectByPrimaryKey(teacherReleaseTask.getTopicId());
            Teacher teacher = teacherMapper.selectByPrimaryKey(teacherReleaseTask.getTeaId());

            String id = teacherReleaseTask.getId();
            Example e = new Example(StuTaskStatus.class);
            Example.Criteria c = e.createCriteria();
            c.andEqualTo("taskId", id);
            c.andEqualTo("stuId", stuId);
            StuTaskStatus stuTaskStatus = stuTaskStatusMapper.selectOneByExample(e);

            map.put("teacherReleaseTask", teacherReleaseTask);
            map.put("topicTitle", topicTitle);
            map.put("teacher", teacher);
            map.put("stuTaskStatus", stuTaskStatus);
            list1.add(map);
        }
        return list1;
    }

    public Map<String, Object> stuSelTopicById(String id, String stuId) {
        Map<String, Object> map = new HashMap<>();
        TeacherReleaseTask teacherReleaseTask = teacherReleaseTaskMapper.selectByPrimaryKey(id);
        TopicTitle topicTitle = topicTitleMapper.selectByPrimaryKey(teacherReleaseTask.getTopicId());
        Teacher teacher = teacherMapper.selectByPrimaryKey(teacherReleaseTask.getTeaId());

        String id1 = teacherReleaseTask.getId();
        Example e = new Example(StuTaskStatus.class);
        Example.Criteria c = e.createCriteria();
        c.andEqualTo("taskId", id1);
        c.andEqualTo("stuId", stuId);
        StuTaskStatus stuTaskStatus = stuTaskStatusMapper.selectOneByExample(e);

        map.put("teacherReleaseTask", teacherReleaseTask);
        map.put("topicTitle", topicTitle);
        map.put("teacher", teacher);
        map.put("stuTaskStatus", stuTaskStatus);
        return map;
    }

    public Integer selStatus(String teaId){
        Example e = new Example(StudentSelection.class);
        Example.Criteria c = e.createCriteria();
        c.andEqualTo("state", "0");
        c.andEqualTo("teaId", teaId);
        int i = studentSelectionMapper.selectCountByExample(e);
        return i;
    }

}
